package com.smh;

import com.smh._017_栈_数组.ArrayStack;
import org.junit.jupiter.api.Test;

import java.util.ArrayDeque;
import java.util.Objects;

/**
 * @author shiminghui
 * @date 2025/2/15 16:09
 * @description: TODO
 */
public class _018_栈_练习 {

    public boolean isValid(String s) {
        char[] charArray = s.toCharArray();
        ArrayStack<Character> arrayStack = new ArrayStack<>();
        for (int i = 0; i < charArray.length; i++) {

            if (charArray[i] == '(') {
                arrayStack.push(')');
            } else if (charArray[i] == '{') {
                arrayStack.push('}');
            } else if (charArray[i] == '[') {
                arrayStack.push(']');
            } else if (charArray[i] == ')' || charArray[i] == '}' || charArray[i] == ']') {
                if (arrayStack.isEmpty() || arrayStack.peek() != charArray[i]) {
                    return false;
                } else {
                    arrayStack.pop();
                }
            }

            /*if (charArray[i] == '(' || charArray[i] == '{' || charArray[i] == '[') {
                arrayStack.push(charArray[i]);
            }else {
                if (arrayStack.isEmpty()) {
                    return false;
                }
                if (arrayStack.peek() == '(' && charArray[i] == ')') {
                    arrayStack.pop();
                }else if (arrayStack.peek() == '{' && charArray[i] == '}') {
                    arrayStack.pop();
                }else if (arrayStack.peek() == '[' && charArray[i] == ']') {
                    arrayStack.pop();
                }else {
                    return false;
                }
            }*/
        }
        return arrayStack.isEmpty();
    }

    public int evalRPN(String[] tokens) {
        ArrayDeque<Object> deque = new ArrayDeque<>();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals("+") || tokens[i].equals("-") || tokens[i].equals("*") || tokens[i].equals("/")) {
                if (deque.size() < 2) {
                    return -1;
                }
                Object pop = deque.pop();
                Object pop1 = deque.pop();
                if (pop1 instanceof Integer && pop instanceof Integer) {
                    int i1 = (int) pop1;
                    int i2 = (int) pop;
                    if (tokens[i].equals("+")) {
                        deque.push(i1 + i2);
                    } else if (tokens[i].equals("-")) {
                        deque.push(i1 - i2);
                    } else if (tokens[i].equals("*")) {
                        deque.push(i1 * i2);
                    } else if (tokens[i].equals("/")) {
                        deque.push(i1 / i2);
                    }
                }
            } else {
                deque.push(Integer.parseInt(tokens[i]));
            }
        }
        return (int) deque.pop();
    }


    // 中缀转后缀(无括号)

    @Test
    public void test1() {
        String[] infix = {"(", "3", "+", "4", ")", "*", "2", "-", "1"};
        String[] strings = infixToSuffix(infix);
        for (String string : strings) {
            System.out.print(string);
        }
    }

    public String[] infixToSuffix(String[] infix) {
        ArrayDeque<String> deque = new ArrayDeque<>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < infix.length; i++) {
            String token = infix[i];
            switch (token) {
                case "+", "-", "*", "/" -> {
                    while (!deque.isEmpty() && getPriority(deque.peek()) >= getPriority(token)) {
                        sb.append(deque.pop());
                    }
                    deque.push(token);
                }
                case "(" -> {
                    deque.push(token);
                }
                case ")" -> {
                    while (!deque.isEmpty() && !deque.peek().equals("(")) {
                        sb.append(deque.pop());
                    }
                    if (!deque.isEmpty() && deque.peek().equals("(")) {
                        deque.pop(); // Pop the '('
                    } else {
                        throw new IllegalArgumentException("Mismatched parentheses");
                    }
                }
                default -> {
                    sb.append(token);
                }
            }
        }
        while (!deque.isEmpty()) {
            sb.append(deque.pop());
        }
        return sb.toString().split("");
    }

    private int getPriority(String s) {
        if (s.equals("+") || s.equals("-")) {
            return 1;
        } else if (s.equals("*") || s.equals("/")) {
            return 2;
        } else if (s.equals("(")) {
            return 0;
        } else if (s.equals(")")) {
            return 3;
        } else {
            return -1;
        }
    }
}
